package com.algorithm.tree;


import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;

@Data
class Node{
    private int value;
    private Node left;
    private Node right;

    public Node(int value) {
        this.value = value;
        this.left = null;
        this.right = null;
    }
}

@Slf4j
public class BinarySearchTree {

    public void install(Node node, int value){
        if (value < node.getValue()){
            Node left = node.getLeft();
            if (left!=null){
                install(left,value);
            }
            left.setLeft(new Node(value));
        }else {
            Node right = node.getRight();
            if (right!=null){
                install(right,value);
            }
            right.setRight(new Node(value));
        }
    }

    public boolean search(Node node, int value){
        if (node.getValue()==value){
            return true;
        }else if (value < node.getValue()){
            Node left = node.getLeft();
            if (left!=null){
                search(left,value);
            }
            return false;
        }else {
            if (node.getRight()!=null){
                search(node.getRight(),value);
            }
            return false;
        }
    }

    @Test
    public void test(){
        Node node = new Node(6);
        install(node,5);
        install(node,7);
        install(node,4);
        install(node,8);

        boolean search = search(node, 7);
        log.info(""+search);

    }
}
